<template>
  <div>
    <div style="margin-bottom: 20px">
      真假结果：{{ resultData1 }} 假指纹的概率：{{ resultData2 }}
    </div>
    <div class="ta-search-box pd">
      <el-form
        ref="form"
        :model="formData"
        :rules="rules"
        label-width="100px"
        size="mini"
      >
        <el-form-item label="账号：" prop="userid">
          <el-input disabled v-model="formData.userid" style="width: 340px" />
        </el-form-item>
        <el-form-item label="图片：" prop="">
          <el-upload
            class="upload-demo"
            action=""
            :auto-upload="false"
            :on-change="handleChange"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
            :limit="1"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item class="text-right">
          <el-button type="primary" @click="handSubmit('form')">
            提交
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { checkFingerTruth } from "@/api/modules/fingerprint";
import { baseURL } from "@/api";
export default {
  name: "Otherpage3",
  data() {
    return {
      formData: {},
      rules: {
        userid: [
          {
            required: true,
            message: "账户不能为空!",
            trigger: "blur",
          },
        ],
        pic: [
          {
            required: true,
            message: "图片不能为空!",
            trigger: "blur",
          },
        ],
      },
      fileList: [],
      resultData1: "",
      resultData2: "",
    };
  },
  created() {
    this.formData.userid = localStorage.getItem("userId");
  },
  methods: {
    handSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;

          let imgfile = new FormData();
          imgfile.append("userid", this.formData.userid);
          imgfile.append("pic", this.formData.pic);
          this.fetchAxios(imgfile).then((res) => {
            console.log(res);
            if (res.data.code == 200) {
              this.resultData1 = res.data.obj[0];
              this.resultData2 = res.data.obj[1];
              this.$message({
                message: res.data.message,
                type: "success",
              });
            } else {
              this.$message({
                message: res.data.message,
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
      this.formData.pic = file.raw;
    },
    async fetchAxios(data) {
      return axios({
        method: "post",
        url: baseURL + "/api/fin",
        data: data,
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
h1 {
  padding-top: 25px;
  padding-bottom: 25px;
}
</style>
